<template>
  <div class="classify">
    <!-- 按钮开始 -->
    <el-button
      style="margin-bottom: 0.5em"
      type="primary"
      size="small"
      plain
      @click="showHandler()"
      >新增</el-button
    >
    <!-- 按钮结束 -->
    <!-- 表格开始 -->
    <el-table :data="classify" style="width: 100%">
      <el-table-column type="index" align="center" label="序号">
      </el-table-column>
      <el-table-column prop="name" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="no" align="center" label="编号"> </el-table-column>
      <el-table-column prop="description" align="center" label="描述">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <span
            @click="editHandler(scope.row)"
            style="color: #6bb3ff; cursor: pointer"
            >编辑&nbsp;</span
          >
          <span
            @click="deleteHandler(scope.row.id)"
            style="color: #e97382; cursor: pointer"
            >删除</span
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <!-- form开始 -->
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="栏目名称" prop="name" required>
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="num">
          <el-input v-model="ruleForm.no"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" :rows="2" v-model="ruleForm.description">
          </el-input>
        </el-form-item>
      </el-form>
      <!-- form结束 -->
      <div slot="footer" size="small" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button size="small" type="primary" @click="submitHandler"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      dialogFormVisible: false,
      ruleForm: {
        name: "",
        no: "",
        description: "",
      },
      title: "新增栏目信息",
    };
  },
  computed: {
    ...mapState("classify", ["classify"]),
  },
  methods: {
    ...mapActions("classify", [
      "findAllClassify",
      "saveClassify",
      "deleteClassify",
    ]),
    // 去新增栏目信息
    showHandler() {
      this.dialogFormVisible = true;
    },
    // 提交保存
    submitHandler() {
      this.saveClassify(this.ruleForm).then((res) => {
        if (res.status == 200) {
          this.dialogFormVisible = false;
          this.findAllClassify();
          this.$message({
            type: "success",
            message: res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    // 编辑栏目信息
    editHandler(data) {
      this.title = "编辑栏目信息";
      this.ruleForm = data;
      this.dialogFormVisible = true;
    },
    // 删除栏目信息
    deleteHandler(id) {
      let params = {
        id,
      };
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.deleteClassify(params).then((res) => {
          if (res.status == 200) {
            this.findAllClassify();
            this.$message({
              type: "success",
              message: res.message,
            });
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
          }
        });
      });
    },
  },
  created() {
    this.findAllClassify();
  },
};
</script>

<style scoped>
</style>
